﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using Microsoft.Kinect;
using Microsoft.Samples.Kinect.WpfViewers;
using Coding4Fun.Kinect.Wpf;
using System.Windows.Ink;

namespace FGP_Kpaint
{
    class Command
    {
        MainWindow main;
        Boolean flag1;
        int x;

        public Command(MainWindow main)
        {
            this.main = main;
            flag1 = false;
            x = 0;

        }


        public Boolean Capture()
        {
            if (!flag1)
            {
                main.image2.Source = main.view2;
                main.image2.Visibility = Visibility.Visible;
                flag1 = true;
                return false;
            }
            else
            {
                x++;
                main.progressBar1.Value = x;
                if (x == 100)
                {
                    BitmapSource image = (BitmapSource)main.image2.Source;
                  //  DateTime.Now.AddMinutes(2013);
                //    DateTime.Today.ToString();
                    string s = "";
                    s = "D:\\" + DateTime.Today.DayOfYear + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".jpeg";
                  //  s = "D:\\Na.jpeg";
                    image.Save(s, ImageFormat.Jpeg);
                    flag1 = true;
                    main.image3.Source = new BitmapImage(new Uri(s));
                    main.image2.Visibility = Visibility.Hidden;
                    x = 0;
                    main.progressBar1.Value = 0;
                    return true;
                }
                else return false;
            }



        }


        public void close()
        {
            main.Close();
        }
        public void save()
        {
            //Microsoft.Win32.SaveFileDialog dl1 = new Microsoft.Win32.SaveFileDialog();

            string FileName = "D:\\" + DateTime.Today.DayOfYear + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second+ ".jpeg";
           
            string DefaultExt = ".png";
            string Filter = "image (.png)|*.png";

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            RenderTargetBitmap bitmap = new RenderTargetBitmap(
                (int)main.ink.ActualWidth,
                (int)main.ink.ActualHeight,
                96,
                96,
                PixelFormats.Pbgra32);

            Rect bounds = VisualTreeHelper.GetDescendantBounds(main.canvas1);
            Console.WriteLine(bounds.X + "|" + bounds.Y + "  " + bounds.Width + "|" + bounds.Height);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(main.canvas1);
                ctx.DrawRectangle(vb, null, new Rect(bounds.Location, bounds.Size));
            }
            bitmap.Render(dv);
            BitmapFrame frame = BitmapFrame.Create(bitmap);
            encoder.Frames.Add(frame);
            //     Nullable<bool> result = dl1.ShowDialog();
            using (var stream = File.Create(FileName))
            {
                encoder.Save(stream);
            }

        }

    }
}
